package com.solcoo.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import com.solcoo.dao.UseromrelaDao;
import com.solcoo.dao.YongHuDao;
import com.solcoo.json.GetEmployeeOrderJson;
import com.solcoo.json.ListEmployeeJson;
import com.solcoo.json.ListEmployeeOrderJson;
import com.solcoo.pojo.Useromrela;
import com.solcoo.pojo.YongHu;

@Service
public class UseromrelaService {

	private static final Logger logger = Logger.getLogger(UseromrelaService.class);
	@Resource
	private YongHuDao yongHuDao;
	@Resource
	private UseromrelaDao useromrelaDao;

	// 添加子账户
	public Map<String, Object> addUseromrela(String ownerid, String telephone, String eno) throws Exception {
		YongHu employee = yongHuDao.selectUserByPhone(telephone);
		if(employee == null) {
			return setFailInfo("该二维码不存在账户！");
		} else if(!"3".equals(employee.getLeixing())) {
			return setFailInfo("该二维码不是配送员！");
		}
		
		Useromrela ur = useromrelaDao.getEmployee(employee.getYonghum());
		if(ur != null) {
			if(!ownerid.equals(ur.getOwnerid()))
				return setFailInfo("该配送员已指定商户！");
			else {
				ur.setEno(eno);
				boolean b = useromrelaDao.enable(ur);
				if(!b) {
					logger.error("useromrelaDao.enable返回错误！");
					return setFailInfo("系统异常, 请重试！");
				}
			}
		} else {
			ur = new Useromrela();
			ur.setOwnerid(ownerid);
			ur.setEmployeeid(employee.getYonghum());
			ur.setEnabled("1");
			ur.setEno(eno);
			useromrelaDao.add(ur);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", ur.getId());
		map.put("eno", eno);
		map.put("name", employee.getNicheng());
		map.put("telephone", telephone);
		map.put("employeeid", employee.getYonghum());
		map.put("result", true);
		
		return map;
	}

	// 删除子账户
	public Map<String, Object> deleteUseromrela(Long id) throws Exception {
		boolean b = useromrelaDao.disable(id);

		if(b) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("result", true);
			map.put("msg", "子账户删除成功");
			
			return map;
		} else {
			return setFailInfo("系统异常, 请重试！");
		}
	}

	// 修改子账户
	public Map<String, Object> updateUseromrela(Long id, String eno) throws Exception {
		Useromrela ur = new Useromrela();
		ur.setId(id);
		ur.setEno(eno);
		boolean b = useromrelaDao.update(ur);
		if(b) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("result", true);
			map.put("msg", "子账户修改成功");
			
			return map;
		} else {
			return setFailInfo("系统异常, 请重试！");
		}
	}

	// 获取子账户
	public Map<String, Object> getEmployee(String ownerid, String employeeid) throws Exception {
		Useromrela ur = useromrelaDao.getEnabledEmployee(employeeid);
		if(ur == null) {
			return setFailInfo("该子账户不存在！");
		} else if(!ownerid.equals(ur.getOwnerid())) {
			return setFailInfo("后台数据错误，请与软件开发商联系！");
		} else {
			YongHu employee = yongHuDao.selectUserByYonghum(ur.getEmployeeid());
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", ur.getId());
			map.put("eno", ur.getEno());
			map.put("name", employee.getNicheng());
			map.put("telephone", employee.getShoujih());
			map.put("employeeid", employee.getYonghum());
			map.put("result", true);
			
			return map;
		}
	}

	// 获取子账户列表
	public Map<String, Object> listEmployee(String ownerid) throws Exception {
		List<ListEmployeeJson> myList = useromrelaDao.getEmployeeList(ownerid);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("empNum", myList.size());
		map.put("myList", myList);
		map.put("result", true);
		
		return map;
	}

	// 获取单个子账户订单
	public Map<String, Object> getEmployeeOrder(String ownerid, String employeeid, String date) throws Exception {
		// 如果是配送员点击对账则维护ownerid
		if(ownerid==null || "".equals(ownerid)) {
			Useromrela urExt = useromrelaDao.getEmployee(employeeid);
			if(urExt == null) {
				return setFailInfo("您还未绑定主账户！");
			} else {
				ownerid = urExt.getOwnerid();
			}
		}
		
		// 获取配送员实体
		YongHu psy = yongHuDao.selectUserByYonghum(employeeid);
		String employeeName = psy.getNicheng();
		
		// 获取订单列表
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ownerid", ownerid);
		param.put("employeeid", employeeid);
		param.put("date", date);
		List<GetEmployeeOrderJson> geos = useromrelaDao.getEmployeeOrder(param);
		BigDecimal rechargeSum = new BigDecimal(0d);
		BigDecimal paySum = new BigDecimal(0d);
		if(geos!=null && geos.size()>0) {
			for(GetEmployeeOrderJson geo:geos) {
				if("1".equals(geo.getOrderType())) {
					rechargeSum = new BigDecimal(geo.getAmounts()).add(rechargeSum);
				} else if("2".equals(geo.getOrderType())) {
					paySum = new BigDecimal(geo.getAmounts()).add(paySum);
				}
			}
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("myList", geos);
		map.put("employeeid", employeeid);
		map.put("employeeName", employeeName);
		map.put("rechargeSum", rechargeSum.doubleValue());
		map.put("paySum", paySum.doubleValue());
		map.put("result", true);
		
		return map;
	}

	// 获取子账户对账信息列表
	public Map<String, Object> listEmployeeOrder(String ownerid, String date) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ownerid", ownerid);
		param.put("date", date);
		List<ListEmployeeOrderJson> eos = useromrelaDao.getEmployeeOrderList(param);
		
		// 添加主账户操作(如果以后重构，该处可以试着修改上面获取list中的sql来实现，减少一次数据库交互
		eos.add(useromrelaDao.getOwnerOrderSum(param));
		
		BigDecimal rechargeSum = new BigDecimal(0d);
		BigDecimal paySum = new BigDecimal(0d);
		if(eos!=null && eos.size()>0) {
			for(ListEmployeeOrderJson eo:eos) {
				rechargeSum = new BigDecimal(eo.getRecharge()).add(rechargeSum);
				paySum = new BigDecimal(eo.getPay()).add(paySum);
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("myList", eos);
		map.put("rechargeSum", rechargeSum.doubleValue());
		map.put("paySum", paySum.doubleValue());
		map.put("result", true);
		
		return map;
	}
	
	/**
	 * 设置错误信息
	 */
	private Map<String, Object> setFailInfo(String failInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("result", false);
		map.put("failMsg", failInfo);
		
		return map;
	}
}